﻿using DL.Animation;
using DL.Combat.Base;
using DL.Combat.Base.Data;
using DL.Tools;
using Messegers;
using Tools.Unilts;
using UnityEngine;

namespace DL.Combat
{
    public abstract class CharacterBaseWeaponAttackCombatController : CharacterWeaponCombatBase
    {
        protected CombatActionSO CurrentCombatAction;
        protected ComboActionConfigSO CurrentComboActionConfig;       

        protected Transform LockTarget;
        protected bool CanAttackInput;
        protected int HitComboIndex;
        protected Collider[] Enemies;

        public CharacterBaseWeaponAttackCombatController(ComboActionConfigSO comboActionConfig)
        {
            CurrentComboActionConfig = Object.Instantiate(comboActionConfig);
            CurrentComboActionConfig.InitComboConfig();
            CanAttackInput = true;
            Enemies = new Collider[10];
        }

        public CombatActionSO GetCurrentCombatAction() => CurrentCombatAction;

        protected override void DamagedDetectionOpen()
        {
            WeaponDetectionCast.AttackCastOpen();
        }

        protected override void DamagedDetectionClose()
        {
            UpdateHitIndex();
            WeaponDetectionCast.AttackCastClose();
        }

        protected override void TriggerDamaged(Transform enemy)
        {
            // var damageables = WeaponDetectionCast.GetDamageableObject();
            // Debug.LogError(damageables.Length);
            // foreach (var damageable in damageables)
            // {
            //     damageable.TriggerDamaged(CurrentCombatAction, HitComboIndex, Self);
            //     LockTarget = damageable.Self;
            // }
            
            //招式表,阶段,攻击者,被打者
            Messenger<CombatActionSO, int, Transform, Transform>.Broadcast(MessengerName.MSG_HurtEvt,
                CurrentCombatAction, HitComboIndex, Self, enemy);
            
            // enemy.TryGetComponent<IDamageable>(out var damageable);
            // damageable.TriggerDamaged(CurrentCombatAction, HitComboIndex, Self);
        }
        
        /// <summary>
        /// 看向敌人
        /// 放在Update中执行好一点
        /// </summary>
        public void TryLookEnemy()
        {
            if (LockTarget == null) return;
            if (DevelopmentToos.CalculateDistance(LockTarget, Self) > 5f) return;
            if (Animator.CheckAnimationTag(AnimatorTag.Attack) && Animator.AnimationNormalTime() < .75f)
            {
                Self.Look(DevelopmentToos.CalculateDirection(LockTarget, Self), 10f);
            }
        }

        /// <summary>
        /// 检测附近范围内的目标
        /// </summary>
        public int DetectionRangEnemy()
        {
            return Physics.OverlapBoxNonAlloc(Self.position + (Self.up * .3f),
                CurrentComboActionConfig.detectionBoxRang,
                Enemies, Quaternion.identity, CurrentComboActionConfig.enemiesLayer, QueryTriggerInteraction.Ignore);
        }

        public void FindNearestEnemy()
        {
            Transform nearstEnemy = null;

            float maxDistance = 5f;

            if (LockTarget != null && Enemies.Length > 0)
            {
                for (int i = 0; i < Enemies.Length; i++)
                {
                    float distance = DevelopmentToos.CalculateDistance(Enemies[i].transform, Self);
                    if (maxDistance < distance)
                    {
                        distance = maxDistance;
                        nearstEnemy = Enemies[i].transform;
                    }
                }
                if (nearstEnemy != null)
                {
                    LockTarget = nearstEnemy;
                }
            }
        }
        
        private void ClearEnemy()
        {
            LockTarget = null;
        }
        
        protected void ChangeComboAction(CombatActionSO config)
        {
            CurrentCombatAction = config;
            ResetHitIndex();
        }
        
        protected void UpdateHitIndex()
        {
            HitComboIndex++;
            //如果更新了招式表，重置一下HitComboIndex
            HitComboIndex = Mathf.Clamp(HitComboIndex, 0, CurrentCombatAction.MaxHitCount - 1);
        }
        
        /// <summary>
        /// 重置HitIndex
        /// </summary>
        protected void ResetHitIndex()
        {
            HitComboIndex = 0;
        }


        /// <summary>
        /// 设置攻击输入是否激活
        /// </summary>
        /// <param name="enable"></param>
        public void SetAttackInputCommand(bool enable)
        {
            CanAttackInput = enable;
        }
        
        /// <summary>
        /// 更新Combat信息(重置CD，激活攻击输入，关闭武器检测)
        /// </summary>
        public void UpdateCombat()
        {
            SetAttackInputCommand(true);
            
            WeaponDetectionCast.AttackCastClose();
        }

        public void DetectionRangDisplay()
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireCube(Self.position + (Self.up * .3f),  CurrentComboActionConfig.detectionBoxRang * 2);
        }
    }
}